//
//  RankViewController.m
//  Coco_Music
//
//  Created by K。 on 15/6/19.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "RankViewController.h"

@interface RankViewController ()

@end

@implementation RankViewController
-(void)dealloc
{
    [_rankTableView release];
    [_bigArray release];
    [_songArray release];
    [_pic_urlArray release];
    [_songListArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    [self getData];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载,请稍后";
    [self.HUD show:YES];
    
  
   
}
-(void)headerRefesh
{
    self.myTable.header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载,请稍后";
        [self.HUD show:YES];
        [self getData];
        [self.myTable reloadData];
        [self.HUD hide:YES];
        //拿到当前的下拉刷新控件,结束刷新状态
        [self.myTable.header endRefreshing];
        
        
    }];
}
-(void)createTable
{
    self.rankTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64-69) style:UITableViewStylePlain];
    self.rankTableView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.rankTableView];
    self.rankTableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
    self.rankTableView.delegate =self;
    self.rankTableView.dataSource =self;
    [self.rankTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
     [_rankTableView release];
}
-(void)getData
{
    [AFN_block NSString:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=281&app=ttpod&v=v7.9.4.2015052918&uid=" Blokc:^(id request) {
        NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary:request];
        self.bigArray =[NSMutableArray array];
        self.bigArray =[dic objectForKey:@"data"];
        self.songArray =[NSMutableArray array];

        
        for (NSMutableDictionary *bigDic in self.bigArray) {
            Rank *rank =[[Rank alloc]init];
            rank.Rankid =[[bigDic objectForKey:@"id"]integerValue];
            rank.pic_url =[bigDic objectForKey:@"pic_url"];
            rank.big_pic_url =[bigDic objectForKey:@"big_pic_url"];
            rank.details =[bigDic objectForKey:@"details"];
            rank.style =[[bigDic objectForKey:@"style"]integerValue];
            rank.time =[bigDic objectForKey:@"time"];
            rank.title =[bigDic objectForKey:@"title"];
            rank.type =[[bigDic objectForKey:@"type"]integerValue];
            rank.songlist =[bigDic objectForKey:@"songlist"];
            for (NSMutableDictionary *songDic in rank.songlist) {
                rank.songName =[songDic objectForKey:@"songName"];
                rank.singerName =[songDic objectForKey:@"singerName"];
            }
    
            [self.songArray addObject:rank];
            [rank release];
            [self createTable];
        
        }
   
        [self headerRefesh];
    }];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
      [self.rankTableView deselectRowAtIndexPath:indexPath animated:YES];
    RankDetailViewController *RankDTC =[[RankDetailViewController alloc]init];
    RankDTC.rank =[self.songArray objectAtIndex:indexPath.row];
    RankDTC.RankDetailID = [[self.songArray objectAtIndex:indexPath.row]Rankid];
  
    [self.navigationController pushViewController:RankDTC animated:YES];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.songArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    RankCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil) {
        cell =[[RankCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
   
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    cell.rank = [self.songArray objectAtIndex:indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ([UIScreen mainScreen].bounds.size.height+100)/5+10;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
